package com.xhwl.logistics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.logistics.bo.VehicleBlacklistBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.CreateVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.UpdateVehicleBlacklistBO;
import com.xhwl.logistics.converter.ToPageBO;
import com.xhwl.logistics.converter.ToPageVO;
import com.xhwl.logistics.converter.mapstruct.VehicleBlacklistConverter;
import com.xhwl.logistics.dto.manipulate.VehicleBlacklistCreateDTO;
import com.xhwl.logistics.dto.manipulate.VehicleBlacklistDeleteDTO;
import com.xhwl.logistics.dto.manipulate.VehicleBlacklistUpdateDTO;
import com.xhwl.logistics.dto.query.VehicleBlacklistDTO;
import com.xhwl.logistics.dto.query.VehicleBlacklistDetailDTO;
import com.xhwl.logistics.service.business.VehicleBlacklistBusinessService;
import com.xhwl.logistics.vo.PageVO;
import com.xhwl.logistics.vo.VehicleBlacklistVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.List;

//@RestController
public class VehicleBlacklistController {

    @Autowired
    private VehicleBlacklistConverter vehicleBlacklistConverter;

    @Autowired
    private ToPageBO toPageBO;

    @Autowired
    private ToPageVO toPageVO;

    @Autowired
    private VehicleBlacklistBusinessService vehicleBlacklistBusinessService;

    @PostMapping("/blacklist/list")
    public ResultJson<PageVO<VehicleBlacklistVO>> query(@RequestBody VehicleBlacklistDTO vehicleBlacklistDTO) {
        QueryVehicleBlacklistBO queryVehicleBlacklistBO = vehicleBlacklistConverter.vehicleBlacklistDTO2QueryVehicleBlacklistBO(vehicleBlacklistDTO);
        PageBO pageBO = toPageBO.toPageBOForBlacklist(vehicleBlacklistDTO);

        IPage<VehicleBlacklistBO> vehicleBlacklistBOIPage = vehicleBlacklistBusinessService.queryBlacklist(queryVehicleBlacklistBO, pageBO);

        List<VehicleBlacklistVO> vehicleBlacklistVOList = vehicleBlacklistConverter.vehicleBlacklistBOList2VehicleBlacklistVOList(vehicleBlacklistBOIPage.getRecords());
        return ResultJson.successWithResult(toPageVO.toPageVO(vehicleBlacklistBOIPage, vehicleBlacklistVOList));
    }

    @PostMapping("/blacklist/detail")
    public ResultJson<VehicleBlacklistVO> detail(@RequestBody VehicleBlacklistDetailDTO vehicleBlacklistDetailDTO) {
        VehicleBlacklistBO vehicleBlacklistBO = vehicleBlacklistBusinessService.detailBlacklist(vehicleBlacklistDetailDTO.getId());

        VehicleBlacklistVO vehicleBlacklistVO = vehicleBlacklistConverter.vehicleBlacklistBO2VehicleBlacklistVO(vehicleBlacklistBO);
        return ResultJson.successWithResult(vehicleBlacklistVO);
    }

    @PostMapping("/blacklist/add")
    public ResultJson<Void> add(@Valid @RequestBody VehicleBlacklistCreateDTO vehicleBlacklistCreateDTO, Account account) {
        CreateVehicleBlacklistBO createVehicleBlacklistBO = vehicleBlacklistConverter.toCreateVehicleBlacklistBO(vehicleBlacklistCreateDTO, account.getId(), account.getName());

        vehicleBlacklistBusinessService.addBlacklist(createVehicleBlacklistBO);

        return ResultJson.successWithVoid();
    }

    @PostMapping("/blacklist/delete")
    public ResultJson<Void> delete(@Valid @RequestBody VehicleBlacklistDeleteDTO vehicleBlacklistDeleteDTO, Account account) {
        DeleteVehicleBlacklistBO deleteVehicleBlacklistBO = vehicleBlacklistConverter.vehicleBlacklistDeleteDTO2DeleteVehicleBlacklistBO(vehicleBlacklistDeleteDTO, account.getId(), account.getName());

        vehicleBlacklistBusinessService.deleteBlacklist(deleteVehicleBlacklistBO);

        return ResultJson.successWithVoid();
    }

    @PostMapping("/blacklist/update")
    public ResultJson<Void> update(@Valid @RequestBody VehicleBlacklistUpdateDTO vehicleBlacklistUpdateDTO, Account account) {
        UpdateVehicleBlacklistBO updateVehicleBlacklistBO = vehicleBlacklistConverter.vehicleBlacklistUpdateDTO2UpdateVehicleBlacklistBO(vehicleBlacklistUpdateDTO, account.getId(), account.getName());

        vehicleBlacklistBusinessService.updateBlacklist(updateVehicleBlacklistBO);

        return ResultJson.successWithVoid();
    }

}
